/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uniq.purchase;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 *
 * @author user
 */
public class Order implements IOrder
{
    public String uid;
    private Map<String,Product> map;

    public Order(String uid) {
        this.uid = uid;
         map = new HashMap();
    }

    @Override
    public boolean addProduct(Product p) 
    {
    if(map.containsKey(p.getArticleNo()))
    {
        Product p1= map.get(p.getArticleNo());
        p1.setPrice(p1.getPrice()+p.getPrice());
        p1.setQty(p1.getQty()+p.getQty());
        return true;
        
    }
    map.put(p.getArticleNo(),p);
    return false;
    }

    @Override
    public boolean removeProduct(String pid) throws ProductNotFoundException {
       if(map.containsKey(pid)){
            map.remove(pid);
            return true;
        }else throw new ProductNotFoundException(
      "Product with ID "+pid+" is not Found.");
    }

    @Override
    public Collection getcartDetails() {
         return map.values();
    }

    @Override
    public Product getProductFromCart(String pid) throws ProductNotFoundException {
         if(map.containsKey(pid)){
            return map.get(pid);
        }else {
            throw new ProductNotFoundException(
        "Product with ID "+pid+" is not Found.");
        }
    }

    @Override
    public double getCartPrice() {
     double price = 0.0d;
    Iterator<Product> iterator = getcartDetails().iterator();
    while(iterator.hasNext()){
      price+= iterator.next().getPrice();
    }
    return price;
    }

    @Override
    public int getProductCount() {
          return map.size();
    }
    
    
    
}
